//简陋图片上传工具

var IS_IE9 = IS_IE9 || false;

var lowBrowserSet = {
	tag:'lowBrowser',
	formID:'lowBrowserFormLoader'
}



var BMXimg = {
	loading:function(){
		var loading = {};
		var $htmlTpl = $("<p id='BMXloading'>上传中</p>");
		if($('body #BMXloading').length<=0){
			$('body').append($htmlTpl);
			$('#BMXloading').css({
				'position': 'fixed',
				'top': '40%',
				'left': '30%',
				'width': '40%',
				'text-align': 'center',
				'font-size': '2rem',
				'margin': '0',
				'padding': '20px',
				'border-radius': '10px',
				'background-color': 'rgba(0, 0, 0, .85)',
				'color': '#ddd',
			});
		}
		
		loading.show = function(){
			$('#BMXloading').show();
		}
		loading.hide = function(){
			$('#BMXloading').hide();
		}
		return loading;
	},
	loadTo:function(file,callback){
		//console.log(file);
		$.ajax({
			url: _root+'../base/picture/',
			type: 'POST',
			dataType: 'json',
			data: file,
			contentType:false,
			processData: false,
			success:callback,
			error:function(d){
				console.log(d);
				console.log('picUpload error');
				BMXimg.loading().hide();
			}
		});	
	},
	
	newLoader:function(config){
		if(IS_IE9){
			var insertPlace = config.insertPlace || $("body");
			var insertClass = config.insertClass || '';
			//cao
			var form = $( "<form method='post' enctype='multipart/form-data' ></form>" ).appendTo(insertPlace);
			var ieUploader = $("<input name='picture' type='file' />")
			form.append($("<input type='hidden' name='"+lowBrowserSet.tag+"' value='1' />"))
			form.append(ieUploader)
			//!!!

			config.$uploader = ieUploader
		}
		

		var picUpload = {
			//参数
			$button:config.$button,
			$picsArea:config.$picArea,
			$uploader:config.$uploader,
			$viewImgTpl:config.$viewImgTpl,
			imgType:config.imgType,


			





			//操作
			fileConfig:{
				maxSize:1024*1024*5,
				type:"image",
				check:function(file){
					if(file.size>this.maxSize){
						alert("您上传的图片过大");
						return 0;
					}
					//if(file.type.indexOf(this.type)){
					//	alert("请上传图片");
					//	return 0;
					//}
					return 1;
				}
			},
			bind:function(callback){
				this.$button.unbind('click');
				this.$button.click(function(event) {
					console.log($(event.target));
					picUpload.eventStack.addImgClick = $(event.target);
					picUpload.$uploader.trigger('click');
					
				});
				picUpload.$uploader.unbind('change');
				picUpload.$uploader.on('change', function(event) {
					event.preventDefault();
					if(IS_IE9){
						console.log(picUpload.$uploader.val())
						var filePath = picUpload.$uploader.val();
						//先不check了
						
						//var uploaderCopy =  $("<input name='picture' type='file' />").val( picUpload.$uploader.val() ).appendTo(form);

						var seed = Math.floor(Math.random() * 1000);
						var id = "uploader-frame-" + seed;
						var iframe = $('<iframe id="'+id+'" name="'+id+'" style="display:none;">');
						var url = _root+'../base/picture/';
						console.log(id,iframe,url)
						form.attr({
							'target': id,
							'action': url + '?' + lowBrowserSet['tag']
						});
						config.insertPlace.append(iframe)
						console.log(iframe)

						window[lowBrowserSet.tag] = function(data){
						　　console.log('received callback:', data);
						　　iframe.remove(); //removing iframe
						　　form.removeAttr('target');
						　　form.attr('action', url);
						　　window[lowBrowserSet.tag] = undefined; //removing callback


							//go
							console.log(data);
							BMXimg.loading().hide();
							if(data.state==200){
								var $e = picUpload.eventStack.addImgClick;
								callback(data,$e);	
							}else{
								alert('上传失败，请尝试在更换浏览器打开本页面上传..')
							}


							//
						};

						form.submit();
						BMXimg.loading().show();
					}else{
						console.log('files:',picUpload.$uploader.get(0).files);
						//alert(picUpload.$uploader.get(0).files)
						if(picUpload.$uploader.get(0).files == undefined){
							alert('为了安全起见，请使用现代浏览器上传图片 ie11,chrome,firefox')
							return false;
						}
						var file = picUpload.$uploader.get(0).files[0];
						
						if(!picUpload.fileConfig.check(file)){
							return;
						}
		
						var formData = new FormData();
						formData.append('picture', file);
						formData.append('type',config.imgType);
						var $e = picUpload.eventStack.addImgClick;
						console.log($e);
						BMXimg.loading().show();
						BMXimg.loadTo(formData,function(d){
							console.log(d);
							BMXimg.loading().hide();
							if(d.state==200){
								callback(d,$e);	
							}else{
								alert('上传失败，请尝试在更换浏览器打开本页面上传')
							}
							
						});
					}
					
				});
			},
			eventStack:{
				$addImgClick:{}
			},
			init:function(){
				this.bind();
				//console.log(picUpload.$viewImgTpl);
			},
			then:function(callback){
				this.bind(callback);
			}
		};
		picUpload.init();
		return picUpload;
	},
	
	init:function(config){
		return this.newLoader(config);
	}
};


